package itcast.bos.base.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import itcast.bos.base.action.base.BaseAction;
import itcast.bos.base.service.PromotionService;
import itcast.bos.domain.base.Promotion;

@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class PromotionAction extends BaseAction<Promotion> {
	@Autowired
	private PromotionService ps;
	
	//分页显示的查找action
	@Action(value="findByPage",results={@Result(name="success",type="json")})
	public String name() {
		Pageable pa=new PageRequest(page-1, rows); 
		Page<Promotion> list=ps.findPageData(pa);
		pushvaluestack(list);
		System.out.println("返回值---------------"+list);
		return SUCCESS;
	}
	
	
	
	
	// 图片上传的参数
	private File imgFile;
	private String imgFileFileName;
	private String imgFileContentType;

	private File titleImgFile;

	// 添加宣传的action
	@Action(value = "addPromotion", results = {
			@Result(name = "success", type = "redirect", location = "./pages/take_delivery/promotion.html"),
			@Result(name="input",type="redirect",location="./error.jsp") })
	public String addPromotion() throws Exception {
		String con_path = ServletActionContext.getRequest().getContextPath() + "/upload/"; // 相对
		String real_path = ServletActionContext.getServletContext().getRealPath("/upload/");
		String s1 = UUID.randomUUID().toString();
		String s2 = titleImgFile.getName().substring(titleImgFile.getName().indexOf("."));
		String destName = s1 + s2;
		// IO流复制
		FileUtils.copyFile(titleImgFile, new File(real_path + "/"+destName));
		System.out.println("上传图片成功。。。"+con_path + destName);
		model.setTitleImg(con_path + destName);
		// 保存宣传
		ps.save(model);

		return SUCCESS;
	}

	// 上传图片的action
	@Action(value = "uploadimg", results = { @Result(name = "success", type = "json") })
	public String uploadImg() throws Exception {
		System.out.println("file-----" + imgFile);
		System.out.println("imgFileFileName-----" + imgFileFileName);
		System.out.println("imgFileContentType-----" + imgFileContentType);
		// 拿到文件的绝对路径和相对路径
		String con_path = ServletActionContext.getRequest().getContextPath() + "/upload"; // 相对
		String real_path = ServletActionContext.getServletContext().getRealPath("/upload");
		// System.out.println("");//绝对
		// 给上传的文件给一个不会重复的名字
		String s1 = UUID.randomUUID().toString();
		String s2 = imgFileFileName.substring(imgFileFileName.indexOf("."));
		String destName = s1 + s2;
		// IO流复制
		FileUtils.copyFile(imgFile, new File(real_path + "/", destName));
		System.out.println("上传图片成功。。。");
		// 告诉浏览器
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("error", 0); // 错误返回o
		result.put("url", con_path + "/" + destName); // 成功返回图片在服务器中的相对路径地址
		System.out.println("图片地址----------------------" + con_path + "/" + destName);
		ActionContext.getContext().getValueStack().push(result);

		return SUCCESS;
	}

	// 管理图片的action
	@Action(value = "managerimg", results = { @Result(name = "success", type = "json") })
	public String managerImg() {
		System.out.println("管理图片了");
		// 获取文件夹的绝对路径
		String rootPath = ServletActionContext.getServletContext().getRealPath("/") + "upload/";
		// 获取文件夹的相对路径
		String cont_path = ServletActionContext.getRequest().getContextPath() + "/upload/";
		// 图片格式的数组
		String[] imgs = { "gif", "jpg", "jpeg", "png", "bmp","tmp" };

		List<Map<String, Object>> fileList = new ArrayList<Map<String, Object>>();
		// 当前路径的文件
		File currageFile = new File(rootPath);
		File[] listFiles = currageFile.listFiles();
		// 对目录下的文件进行便利
		if (listFiles != null) {
			for (File f : listFiles) {
				Map<String, Object> map = new HashMap<String, Object>();
				String fname = f.getName();
				if (f.isDirectory()) {
					// 文件夹
					map.put("is_dir", true);
					map.put("has_file", (f.listFiles() != null));
					map.put("filesize", 0L);
					map.put("is_photo", false);
					map.put("filetype", "");
				} else if (f.isFile()) {
					// 文件 判断是否是图片 先拿到文件的后缀名 并小写
					String ext = f.getName().substring(f.getName().lastIndexOf(".") + 1).toLowerCase();
					System.out.println("文件后缀名------" + ext);
					map.put("is_dir", false);
					map.put("has_file", false);
					map.put("filesize", f.length());
					map.put("is_photo", Arrays.<String> asList(imgs).contains(ext));
					map.put("filetype", ext);
				}
				map.put("filename", fname);
				map.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(f.lastModified()));
				fileList.add(map);
			}
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("moveup_dir_path", "");
		result.put("current_dir_path", rootPath);
		result.put("current_url", cont_path);
		result.put("total_count", fileList.size());
		result.put("file_list", fileList);
		ActionContext.getContext().getValueStack().push(result);

		return SUCCESS;
	}

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public String getImgFileFileName() {
		return imgFileFileName;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	public String getImgFileContentType() {
		return imgFileContentType;
	}

	public void setImgFileContentType(String imgFileContentType) {
		this.imgFileContentType = imgFileContentType;
	}

	public File getTitleImgFile() {
		return titleImgFile;
	}

	public void setTitleImgFile(File titleImgFile) {
		this.titleImgFile = titleImgFile;
	}

}
